layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'apply_list',
		form_filter = 'apply_search_form';
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: ajax_apply_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'invoice_id',
			_b: 'desc'
		},
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		cols: [apply_cols]
	});


	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	var select_search_list = function(field, value) {
		var where = {};
		where[field] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	};
	// select 查询
	/*
	form.on('select(builder_type)', function(data) {
		select_search_list('builder_type', data.value);
	})
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search_list(name, value);
	});
	*/

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var invoice_id = data.invoice_id,
			invoice_money = data.invoice_money;
		switch (e) {
			case 'apply_invoice':
				if (data.invoice_type == 1) {
					$('.invoice_type3').hide();
					$('.invoice_type3 input').attr('lay-verify','');
				} else {
					$('.invoice_type3').show();
					$('.invoice_type3 input').attr('lay-verify','required');
				}
				form.val('tpl_add_invoice_info', data);
				// 处理特例
				$('input[name="invoice_type"][value="'+data.invoice_type+'"]').prop('checked',true);
				form.render();
				$('input[name="invoice_money"]').val(invoice_money);
				$('input[name="invoice_id"]').val(invoice_id);
				layer.open({
					content: $('#invoice_info'),
					type: 1,
					anim: 2,
					title: '开票信息',
					closeBtn: true,
					shadeClose: true,
					area: ['650px', '650px'],
					shade: 0.1
				})
				break;
			default:
				break;
		}
	})
	form.on('radio(invoice_type)', function(data) {
		if (data.value == 3) {
			$('.invoice_type3').show(300);
			$('.invoice_type3 input').attr('lay-verify','required');
		} else {
			$('.invoice_type3').hide(300);
			$('.invoice_type3 input').attr('lay-verify','');
		}
	}) 
	form.on('submit(submit_invoice)', function(data) {
		var field = data.field;
		field.field = 'apply_state';
		field.value = 1;
		var url = $('form[lay-filter="tpl_add_invoice_info"]').attr('action');
		layer.confirm('确认要申请开具发票吗？', {
			icon: 3,
			title: '申请开票'
		}, function(index) {
			send_info(url, field, function(data) {
				table.reload('apply_list');
				parent.layui.table.reload('invoice_list');
				parent.layer.msg('申请开票成功', {
					icon: 1,
					anim: 6
				});
				location.reload();
			})
		})
	})
})
